package com.xoa.plugin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.xoa.dao.attend.AttendEvectionMapper;
import com.xoa.dao.attend.AttendLeaveMapper;
import com.xoa.dao.flowhook.FlowHookMapper;
import com.xoa.dao.workflow.FlowProcessMapper;
import com.xoa.global.proxy.TriggerSubject;
import com.xoa.model.attend.AttendEvection;
import com.xoa.model.attend.AttendLeave;
import com.xoa.model.workflow.FlowHookWithBLOBs;
import com.xoa.model.workflow.FlowProcess;
import com.xoa.model.workflow.TriggerPlugIn;
import com.xoa.util.DateFormat;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gsb on 2017/8/5.
 */
@Service
public class AttendEvectionTrigger implements TriggerSubject {
    @Autowired
    private FlowHookMapper flowHookMapper;
    @Autowired
    private AttendEvectionMapper evectionMapper;
    @Resource
    private FlowProcessMapper flowProcessMapper;

    @Override
    public void doRun(String company,Object... ags) {
        ContextHolder.setConsumerType(company);
        TriggerPlugIn tri = (TriggerPlugIn) ags[0];
        AttendEvection attendEvection=new AttendEvection();

        FlowProcess flowProcess= flowProcessMapper.findPrcessLine(Integer.parseInt(tri.getFlowId()),Integer.parseInt(tri.getCurrentPrcsId()));

        if(flowProcess!=null) {
            String prcs = flowProcess.getPrcsTo();
            if (!"".equals(prcs) && prcs != null) {
                prcs = prcs.substring(0, prcs.length() - 1);
                if ("0".equals(prcs)) {
                    attendEvection.setAllow("1");


                } else {

                    attendEvection.setAllow("0");

                }
            }
        }
        FlowHookWithBLOBs flowHookNew=new FlowHookWithBLOBs();
        flowHookNew.setHid(tri.getHookId());
        FlowHookWithBLOBs flowHook=  flowHookMapper.selectByhid(flowHookNew);
        Map<Object,Object> flowHookMaps=null;
        if(flowHook!=null){
            flowHookMaps=queryTriggerField(tri.getModify(),tri.getReject(),flowHook.getMap(),flowHook.getDataDirection());
        }

        if(flowHookMaps!=null) {
            for (Map.Entry<Object, Object> entry : flowHookMaps.entrySet()) {
                if (entry.getKey().equals("LEADER_ID")) {
                    attendEvection.setLeaderId(entry.getValue().toString());
                } else if (entry.getKey().equals("EVECTION_DEST")) {
                    attendEvection.setEvectionDest(entry.getValue().toString());
                } else if (entry.getKey().equals("EVECTION_DATE1")) {
                    attendEvection.setEvectionDate1(DateFormat.getDate(entry.getValue().toString()));
                } else if (entry.getKey().equals("EVECTION_DATE2")) {
                    attendEvection.setEvectionDate2(DateFormat.getDate(entry.getValue().toString()));
                }else if (entry.getKey().equals("REASON")) {
                    attendEvection.setReason(entry.getValue().toString());
                }
            }
            attendEvection.setRunId(Integer.parseInt(tri.getRunId()));
            int a=evectionMapper.updateByExampleSelective(attendEvection);
        }

    }

    public  Map<Object,Object> queryTriggerField(String modifydata,String fromdate,String maps,Integer num){

        //获取json串 进行判断
        //获取业务引擎接口的数据进行分开，组合map
        /**
         * 拿自己内置的数据去写的
         */

        String[] a=maps.split(",");
        Map<String,Object> map=new HashMap<String,Object>();
        for (int i=0;i<a.length;i++){
            String i1=a[i];
            String i2[]=i1.split("=>");
            String i3=i2[0];
            String i4=i2[1];
            map.put(i3,i4);
            //key           USER_ID
            //value         申请人

        }
        JSONArray json = new JSONArray();
        //String fromdateOne="["+fromdate+"]";
        Map<String, Object> from = json.parseObject(
                fromdate,new TypeReference<Map<String, Object>>(){} );


        List<Map<String, Object>> modify = json.parseObject(modifydata, List.class);
        //List<Map<String, Object>> from = json.parseObject(fromdate, List.class);
        Map<Object,Object> analysis=new HashMap<Object,Object>();
        Map<Object,Object> returnMap=new HashMap<Object,Object>();
        for (Map<String, Object> mapmodify : modify) {
            for (Map.Entry<String,Object> entry1 : from.entrySet()){
                if(mapmodify.get("key").equals(entry1.getValue())){
                    analysis.put(entry1.getKey(),mapmodify.get("value"));
                }

            }                        // 申请人   value   USER_ID
        }

        if("1".equals(num)){
            for (Map.Entry<Object,Object> entry : analysis.entrySet()) {//key 申请人 value  1
                for (Map.Entry<String,Object> entry1 : map.entrySet()) {
                    if (entry.getKey().equals(entry1.getValue())) {
                        returnMap.put(entry1.getKey(),entry.getValue());

                    }

                }

            }
        }else if("2".equals(num)){

        }
        for (Map.Entry<Object,Object> entry : analysis.entrySet()) {//key 申请人 value  1
            for (Map.Entry<String,Object> entry1 : map.entrySet()) {
                if (entry.getKey().equals(entry1.getKey())) {
                    returnMap.put(entry1.getValue(),entry.getValue());

                }

            }


        }



        return  returnMap;
    }
}
